<?php
class PostsController extends AppController {

	var $name = 'Posts';
	var $helpers = array('Html', 'Form','Ajax','Javascript');
	 var $components = array('RequestHandler');
	 
	function beforeFilter() {
		parent::beforeFilter(); 
		$this->Auth->allowedActions = array('index');
	}
/*Todos*/	
/*Lista los posts de un topico con $id*/
	function index($id = null) {
		
		$topico = $this->Post->Topico->find('first', array('conditions' => array('Topico.topico =' => $id)));
		$posts = $this->Post->find('all', array('conditions' => array('Post.topico_id =' => $id)));
		//$posts = $this->paginate('Post', array('Post.topico_id =' => $id));
		//print_r($tema);
		if (!$topico) {
			$this->Session->setFlash(__('Topico invalido', true));
			$this->redirect(array('controller'=> 'temas','action'=>'index'));
		}
		$this->set('grupo_id', $this->Auth->user('grupo_id'));//el grupo del user
		$this->set('posts', $posts);// todos los posts relacionados con el topico
		$this->set('topico', $topico['Topico']); //se envia la fila post relacionado con el topico
	}
/*
	function view($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Post', true), array('action'=>'index'));
		}
		$this->set('post', $this->Post->read(null, $id));
	}
*/

/*A partir de Trial!! permitir con ACL*/	
/*Para agregar nuevos posts en el topico*/
	function add($topicoid =null){
		if (!$topicoid && empty($this->data)) {
			$this->Session->setFlash(__('Topico Invalido', true));
		}
		if(!$this->Auth->user()){
			$this->Session->setFlash(__('Para agregar posts es necesario loguearse', true));
			exit();
		}
		if (!empty($this->data)) {
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('El post ha sido guardado', true));
			} else {
				$this->Session->setFlash(__('El post no pudo ser guardado. Por favor intentelo de vuelta', true));
			}
		} else {
			$this->data['Post']['topico_id'] = $topicoid;
		}
	}
/*
	function add($topicoid = null) {
		echo $this->Auth->user('username');
		if(!$this->Auth->user()){
			$this->Session->setFlash(__('Para agregar posts es necesario loguearse', true));
			$this->redirect(array('controller' =>'posts','action'=>'index',$topicoid));
		}
		if($this->Auth->user('grupo_id') > 3){ //Si el usuario es trial o premium
			$this->Session->setFlash(__('Solo un usuario Trial , Premium o Administrador puede crear nuevos posts', true));
			$this->redirect(array('controller' =>'posts','action'=>'index',$topicoid));
		}
		
		//$elTopico = $this->Post->Topico->find('first', array('conditions' => array('Topico.topico =' => $topicoid)));
		//print_r($elTopico);
		$elTopico=$this->Post->Topico->findByTopico($topicoid);
		if(!$elTopico && empty($this->data)){ //si no existe el tema, y no se completo el formulario
			$this->Session->setFlash(__('El topico no existe.', true));
			$this->redirect(array('controller' => 'temas','action'=>'index'));
		}
		if (!empty($this->data)) {
			$this->data['Post']['user_id']=$this->Auth->user('user');
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('El post ha sido guardado', true));
				$this->redirect(array('action'=>'index', $this->data['Post']['topico_id']));
			} else {
				$this->Session->setFlash(__('El post no pudo ser guardado. Por favor intentelo de vuelta', true));
			}
		}
		$this->set('elTopico',$elTopico['Topico']);
	}
*/
/*Solo el adminsitrador puede editar los posts*/
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->flash(__('Post invalido', true), array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->flash(__('Su Post ha sido guardado.', true), array('action'=>'index'));
			} else {
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$topicos = $this->Post->Topico->find('list');
		$users = $this->Post->User->find('list');
		$this->set(compact('topicos','users'));
	}
	
/*Solo el administrador puede borrar posts.. controlado por acl*/
	function delete($id = null,$topicoId = null) {
		if (!$id) {
			$this->Session->setFlash(__('Post Invalido', true));
			$this->redirect(array('action'=>'index',$topicoId));
		}
		if ($this->Post->del($id)) {
			$this->Session->setFlash(__('Post Borrado', true));
			$this->redirect(array('action'=>'index',$topicoId));
		}
	}
}
?>